package com.atguigu.daijia.customer.controller;

import com.atguigu.daijia.common.login.GuiguLogin;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "客户API接口管理")
@RestController
@RequestMapping("/customer")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerController {

	@Autowired
	private CustomerService customerInfoService;


	//获取客户登录信息(新方法)
	@Operation(summary = "获取客户登录信息")
	@GuiguLogin//添加自定义注解,用来校验登录,登录成功才有权限访问该方法功能
	@GetMapping("/getCustomerLoginInfo")
	public Result<CustomerLoginVo> getCustomerLoginInfo() {

		//1.从ThreadLocal获取用户id
		Long customerId = AuthContextHolder.getUserId();

		//调用service
		CustomerLoginVo customerLoginVo = customerInfoService.getCustomerInfo(customerId);

		return Result.ok(customerLoginVo);
	}

	/*//获取客户登录信息(旧方法)
	@Operation(summary = "获取客户登录信息")
	@GetMapping("/getCustomerLoginInfo")
	public Result<CustomerLoginVo> getCustomerLoginInfo(@RequestHeader(value = "token") String token) {
		//1 从请求头获取token字符串
        //HttpServletRequest request
        //String token = request.getHeader("token");

		//调用service
		CustomerLoginVo customerLoginVo = customerInfoService.getCustomerLoginInfo(token);

		return Result.ok(customerLoginVo);
	}*/

	//小程序授权登录
	@Operation(summary = "小程序授权登录")
	@GetMapping("/login/{code}")
	public Result<String> wxLogin(@PathVariable String code) {
		return Result.ok(customerInfoService.login(code));
	}

	//更新用户微信手机号
	@Operation(summary = "更新用户微信手机号")
	@GuiguLogin
	@PostMapping("/updateWxPhone")
	public Result updateWxPhone(@RequestBody UpdateWxPhoneForm updateWxPhoneForm) {
		updateWxPhoneForm.setCustomerId(AuthContextHolder.getUserId());
		//微信小程序个人版的获取不到手机号，这一步直接跳过
		//customerInfoService.updateWxPhoneNumber(updateWxPhoneForm);
		return Result.ok(true);
	}
}

